<?php
include_once "BaseM.php";
 
class BloodMessageM extends BaseM{
	
	private $table = "blood_message";
	
	public function __construct() {
		parent::__construct ();
	}
	
	public function insert($obj) {
		$sql = $this->genInserSQL($this->table, $obj);
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $result;
	}

	public function update($obj) {	  
		$sql = $this->genUpdateSQL($this->table, $obj, array("id"));
		$result = @mysql_query($sql) or die ("sql : ".$sql.'\n'.mysql_error ());
		return  $result;	
	}
 
	public function delete($key) {
		$sql="delete from $this->table where id = $key";
		$count = @mysql_query( $sql ) or die ("sql : ".$sql.'\n'.mysql_error ());
		return $count;
	}
	
	public function getTableData($key) {
		$sql = "
			select 
				id, species, blood, date_create,
				(select account from member where id = b.member_id) as memberAccount,
				case species 
					when 1 then '狗'
					when 2 then '貓'
				end species2,
				case (select regist_type from member where id = b.member_id) 
					when 1 then '一般註冊'
					when 2 then 'Facebook註冊'
					when 3 then 'G+註冊'
				end memberRegistType
			from $this->table as b
			where id = $key";
		return $this->querySQL($sql, true);
	}
	
	public function queryTable($orderData, $startPage, $pageRow, $searchStr) {
		$whereStr = " 1 = 1 $searchStr ";
		
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
		
		$resultSql = "
			select 
				id, species, blood, date_create,
				(select account from member where id = b.member_id) as memberAccount,
				case species 
					when 1 then '狗'
					when 2 then '貓'
				end species2,
				case (select regist_type from member where id = b.member_id) 
					when 1 then '一般註冊'
					when 2 then 'Facebook註冊'
					when 3 then 'G+註冊'
				end memberRegistType
			from $this->table as b
			where $whereStr
			order by $orderData 
			limit $startPage, $pageRow ";
		$result = $this->querySQL($resultSql);
		
		return array( "count" => $count->count, "result" => $result );
	}
	
	public function query($startPage, $pageRow){
		$whereStr = " 1 = 1";
	
		$countSql = "select count(id) as count from $this->table where $whereStr ";
		$count = $this->querySQL($countSql, true);
	
		$resultSql = "
			select
				id as BMId, photo, species, blood, date_create as dateCreate
			from $this->table
			where $whereStr
			order by date_create desc
			limit $startPage, $pageRow ";
		$result = $this->querySQL($resultSql);
	
		return array( "count" => $count->count, "result" => $result );
	}
	
	public function getById($id){
		$resultSql = "select id, species, blood, photo, hp_name, hp_address, hp_phone, photo, content from $this->table where id = $id ";
		return $this->querySQL($resultSql, true);
	}
}

?>